package com.gitee.wsl.android.ui.spannable.span

import android.graphics.Rect
import android.text.TextPaint
import android.text.style.AbsoluteSizeSpan

class CustomAbsoluteSizeSpan(
    private val mNormalSizeText: String,
    private val mText: String,
    size: Int,
    private val mTextPaint: TextPaint,
    private val mGravity: Int = BOTTOM
) :AbsoluteSizeSpan(size, true) {
    private var mOffsetBaselineShift = 0
    private val mTextViewRect = Rect()
    private val mTextRect = Rect()
    override fun updateDrawState(ds: TextPaint) {
        super.updateDrawState(ds)
        if (mGravity == BOTTOM) return
        mTextPaint.getTextBounds(mNormalSizeText, 0, mNormalSizeText.length, mTextViewRect)
        ds.getTextBounds(mText, 0, mText.length, mTextRect)
        val mMainTextHeight = mTextViewRect.height()
        val offset = mTextViewRect.bottom - mTextRect.bottom
        when (mGravity) {
            TOP -> mOffsetBaselineShift =
                mMainTextHeight - mTextRect.height() - offset

            CENTER -> mOffsetBaselineShift =
                mMainTextHeight / 2 - mTextRect.height() / 2 - offset
        }
        ds.baselineShift -= mOffsetBaselineShift
    }

    override fun updateMeasureState(ds: TextPaint) {
        super.updateMeasureState(ds)
        ds.baselineShift -= mOffsetBaselineShift
    }

    companion object{
        const val  BOTTOM=1
        const val  TOP=2
        const val  CENTER=3
    }
}